package com.acme.serverside;

import java.io.IOException;
import java.io.InputStream;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.LinkedList;
import java.util.List;
import java.util.Properties;

public class ChatServer {

    public static final String EXIT = "<exit>";
    public static final String ONLINE = "online!";

    private static List<ClientConnection> clientConnections;
    private static ControlMessaging controlMessaging = new ControlMessaging();

    protected static int sizeListClient() {
        return clientConnections.size();
    }

    protected static void sendAll(String nickName, String message) throws IOException {
        long startTime = System.currentTimeMillis();
        if (!controlMessaging.validateParametersSendAll(message)) {
            return;
        }
        for (ClientConnection clientConnection : clientConnections) {
            if (!nickName.equals(clientConnection.getNickName())) {
                if(clientConnection.isActive){
                    clientConnection.sendMessage(controlMessaging.textMessage(nickName, message));
                }
            }
        }
        System.out.println("Time for sending message to all clients: " + (System.currentTimeMillis() - startTime));
    }

    protected static void clientDisconnecting(ClientConnection clientConnection) {
        clientConnections.remove(clientConnection);
        System.out.printf("%s disconnected\n", clientConnection.getNickName());
    }

    public Properties readConfig() {
        Properties configServer = null;
        try (InputStream inputStream = getClass().getClassLoader().getResourceAsStream("config.properties")) {
            configServer = new Properties();
            configServer.load(inputStream);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return configServer;
    }


    public ServerSocket loadProperty() throws IOException {
        Properties config = readConfig();
        int port = Integer.parseInt(config.getProperty("port"));
        InetAddress ip = InetAddress.getByName(config.getProperty("ip"));
        return new ServerSocket(port, 0, ip);
    }

    private void startServer() throws Exception {
        try (ServerSocket serverSocket = loadProperty()) {
            clientConnections = new LinkedList<>();
            System.out.printf("Waiting for connections...\n");
            while (true) {
                Socket socket = serverSocket.accept();
                if (controlMessaging.checkCountClient(socket)) {
                    clientConnections.add(new ClientConnection(socket));
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    protected static boolean existsNickName(String nickName) {
        for (ClientConnection c : clientConnections) {
            if (nickName.equals(c.getNickName())) {
                return true;
            }
        }
        return false;
    }


    public static void main(String[] args) throws Exception {
        ChatServer chatServer = new ChatServer();
        chatServer.startServer();
    }

}
